/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef UINPUT_TOUCH_H
#define UINPUT_TOUCH_H

#include <securec.h>
#include <stdint.h>
#include "osal_time.h"
#include "hdf_input_device_manager.h"
#include "uinput_config_parser.h"
#include "uinput_i2c_ops.h"

#define CHIP_CHECK_RETURN(ret) do { \
    if ((ret) != HDF_SUCCESS) { \
        return ret; \
    } \
} while (0)

#define MAX_FINGERS_NUM 10
#define SELF_TEST_RESULT_LEN 20

#define PWR_TYPE_INDEX      0
#define PWR_STATUS_INDEX    1
#define PWR_DIR_INDEX       2
#define PWR_DELAY_INDEX     3
#define PWR_CELL_LEN        4

typedef struct {
    uint32_t type;
    uint32_t code;
    int32_t value;
    uint64_t time;
} EventPackage;

typedef enum {
    TOUCH_DOWN,
    TOUCH_UP,
    TOUCH_CONTACT,
} EventType;

typedef enum {
    TYPE_UNKNOWN,
    TYPE_VCC,
    TYPE_VCI,
    TYPE_RESET,
    TYPE_INT,
} TimingType;

typedef struct {
    int32_t x;         // x coordinate
    int32_t y;         // y coordinate
    int32_t pressure;
    int32_t trackId;   // touch ID
    int32_t status;    // record every point's status
    bool valid;
} FingerData;

typedef struct {
    FingerData fingers[MAX_FINGERS_NUM];
    int32_t realPointNum;
    int32_t definedEvent;    // touch event: 0-down; 1-up; 2-contact
    OsalTimespec time;
} FrameData;

struct TouchChipDevice;
typedef struct TouchPlatformDriver {
    struct HdfDeviceObject *hdfTouchDev;
    InputDevice *inputDev;
    struct TouchChipDevice *device;
    FrameData frameData;
    uint32_t devType;
    const char *devName;
    TouchBoardCfg *boardCfg;
    InputI2cClient i2cClient;
    struct OsalMutex mutex;
    uint32_t pwrStatus;
    uint32_t gestureMode;
    bool initedFlag;
    bool irqStopFlag;
} TouchDriver;

struct TouchChipOps;
typedef struct TouchChipDevice {
    TouchDriver *driver;
    const char *chipName;
    const char *vendorName;
    struct TouchChipOps *ops;
    TouchChipCfg *chipCfg;
    TouchBoardCfg *boardCfg;
} ChipDevice;

struct TouchChipOps {
    int32_t (*Init)(ChipDevice *device);
    int32_t (*Detect)(ChipDevice *device);
    int32_t (*Resume)(ChipDevice *device);
    int32_t (*Suspend)(ChipDevice *device);
    int32_t (*DataHandle)(ChipDevice *device);
    int32_t (*UpdateFirmware)(ChipDevice *device);
    void (*SetAbility)(ChipDevice *device);
};

typedef struct {
    uint32_t testType;
    char testResult[SELF_TEST_RESULT_LEN];
} CapacitanceTestInfo;

typedef struct {
    const char *cmdCode;
    const char *cmdValue;
} InputExtraCmd;

int32_t RegisterTouchChipDevice(ChipDevice *chipDev);

#define MAX_TOUCH_DEVICE 5

#endif // UINPUT_TOUCH_H
